<template>
  <div>
    <el-button type="success" @click="add">添加</el-button>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="编号"></el-table-column>
      <el-table-column prop="title" label="标题"></el-table-column>
      <el-table-column prop="sn" label="编码"></el-table-column>
      <el-table-column prop="intro" label="简介"></el-table-column>
      <el-table-column label="操作">
        <!-- 作用域插槽<slot 自定义属性1="xxx" 自定义2="xxx"></slot> -->
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="edit(scope)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <!-- 弹窗组件 -->
    <my-dialog :title="dialogTitle" :visible="dialogFormVisible" :form="form" @close="closeDialog"></my-dialog>
  </div>
</template>

<script>
//1.引入组件
import MyDialog from "./children/Dialog";
import {
  getSysdictionary,
  editSysdictionary,
  addSysdictionary
} from "@/request/httpApi";
export default {
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      form: {},
      dialogTitle: "",
      currentPage: 1,
      pageSize: 10,
      total: 0
    };
  },
  //2.注册组件
  components: {
    MyDialog
  },
  methods: {
    closeDialog(reload) {
      this.dialogFormVisible = false;
      if (reload == "reload") {
        //刷新
        this.fetchData();
      }
    },
    //添加按钮
    add() {
      this.form = {};
      this.dialogTitle = "数据字典添加";
      this.dialogFormVisible = true;
    },
    fetchData() {
      getSysdictionary({
        currentPage: this.currentPage,
        pageSize: this.pageSize
      }).then(res => {
        if (res.success) {
          console.log(res);
          this.tableData = res.data.list;
          //总条数
          this.total = res.data.total;
        } else {
        }
      });
    },
    //每页显示的条数改变时会触发
    handleSizeChange(size) {
      this.pageSize = size;
      //重新获取数据
      this.fetchData();
    },
    //页码改变时
    handleCurrentChange(page) {
      console.log(page);
      this.currentPage = page;
      //重新获取数据
      this.fetchData();
    },

    //添加按钮
    add() {
      this.form = {};
      this.dialogTitle = "数据字典添加";
      this.dialogFormVisible = true;
    },
    //编辑按钮
    edit(scope) {
      console.log(scope);
      this.dialogTitle = "数据字典编辑";
      this.form = { ...scope.row };
      this.form = { ...scope.row };
      this.dialogFormVisible = true;
    }
  },
  created() {
    //获取列表数据
    this.fetchData();
  }
};
</script>

<style>
</style>